package com.yisu.expressway.customService.adapter;

import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.yisu.expressway.R;
import com.yisu.expressway.customService.VoiceAssistantActivity;
import com.yisu.expressway.customService.model.HighWayInfoObj;
import com.yisu.expressway.databinding.TextWithArrowForDatabindingBinding;
import com.yisu.expressway.databinding.TwoTextWithRightArrowBinding;
import com.yisu.expressway.listener.onClickHandler;
import com.yisu.expressway.model.ServiceDistrictItem;
import com.yisu.expressway.service_district.DistrictDetailActivity;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/1/2 0002.
 */

public class DistrictListItemAdapter extends RecyclerView.Adapter<DistrictListItemAdapter.ListItemViewHolder> {

    @NonNull
    private ArrayList<ServiceDistrictItem> mData = new ArrayList<>();
    VoiceAssistantActivity mActivity;

    public DistrictListItemAdapter(VoiceAssistantActivity activity){
        mActivity = activity;
    }

    public void update (ArrayList<ServiceDistrictItem> list){
        mData = list;
        notifyDataSetChanged();
    }

    @Override
    public ListItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.two_text_with_right_arrow, parent, false);
        return new ListItemViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(ListItemViewHolder holder, int position) {
        holder.bind(mData.get(position));
    }

    public  class ListItemViewHolder extends RecyclerView.ViewHolder implements onClickHandler {
        private TwoTextWithRightArrowBinding itemBinding;

        public ListItemViewHolder(View view){
            super(view);
            itemBinding = DataBindingUtil.bind(view);
        }

        public void bind(@NonNull ServiceDistrictItem districtItem){
            StringBuilder distanceStr = new StringBuilder(String.valueOf(districtItem.distance));
            distanceStr.append(" ").append("公里");
            itemBinding.setDistrictItem(districtItem);
            itemBinding.tvDistance.setText(distanceStr.toString());
            itemBinding.setClickListner(this);
        }

        @Override
        public void handleClick(View view) {
            ServiceDistrictItem selectItem = itemBinding.getDistrictItem();
            if(selectItem!= null) {
                DistrictDetailActivity.launch(mActivity,selectItem,false);
            }
        }
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }
}
